package com.xuedongzhilian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuedongzhilian.dto.WordStudyDTO;
import com.xuedongzhilian.entity.WordStudy;
import com.xuedongzhilian.mapper.WordStudyMapper;
import com.xuedongzhilian.service.IWordStudyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 单词学习服务实现类
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Service
public class WordStudyServiceImpl implements IWordStudyService {

    @Resource
    private WordStudyMapper wordStudyMapper;

    @Override
    public boolean addWordStudy(WordStudyDTO wordStudyDTO) {
        WordStudy wordStudy = BeanUtil.copyProperties(wordStudyDTO, WordStudy.class);
        wordStudy.setCreateTime(LocalDateTime.now());
        wordStudy.setUpdateTime(LocalDateTime.now());
        return wordStudyMapper.insert(wordStudy) > 0;
    }

    @Override
    public List<WordStudyDTO> getWordStudiesByUserId(Long userId) {
        QueryWrapper<WordStudy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<WordStudy> wordStudies = wordStudyMapper.selectList(queryWrapper);
        return wordStudies.stream()
                .map(wordStudy -> BeanUtil.copyProperties(wordStudy, WordStudyDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public WordStudyDTO getWordStudyDetail(Long wordId, Long userId) {
        QueryWrapper<WordStudy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", wordId).eq("user_id", userId);
        WordStudy wordStudy = wordStudyMapper.selectOne(queryWrapper);
        if (wordStudy == null) {
            return null;
        }
        return BeanUtil.copyProperties(wordStudy, WordStudyDTO.class);
    }

    @Override
    public boolean updateWordStudy(WordStudyDTO wordStudyDTO) {
        WordStudy wordStudy = BeanUtil.copyProperties(wordStudyDTO, WordStudy.class);
        wordStudy.setUpdateTime(LocalDateTime.now());
        return wordStudyMapper.updateById(wordStudy) > 0;
    }

    @Override
    public boolean deleteWordStudy(Long wordId, Long userId) {
        QueryWrapper<WordStudy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", wordId).eq("user_id", userId);
        return wordStudyMapper.delete(queryWrapper) > 0;
    }

    @Override
    public boolean markWordAsMastered(Long wordId, Long userId) {
        WordStudy wordStudy = new WordStudy();
        wordStudy.setId(wordId);
        wordStudy.setIsMastered(1); // 1表示已掌握
        wordStudy.setUpdateTime(LocalDateTime.now());
        QueryWrapper<WordStudy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", wordId).eq("user_id", userId);
        return wordStudyMapper.update(wordStudy, queryWrapper) > 0;
    }

    @Override
    public List<WordStudyDTO> getUnmasteredWords(Long userId, int count) {
        QueryWrapper<WordStudy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("is_mastered", 0); // 0表示未掌握
        queryWrapper.last("LIMIT " + count);
        List<WordStudy> wordStudies = wordStudyMapper.selectList(queryWrapper);
        return wordStudies.stream()
                .map(wordStudy -> BeanUtil.copyProperties(wordStudy, WordStudyDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<WordStudyDTO> getUnmasteredWordsByDifficulty(Long userId, int count, Integer difficulty) {
        QueryWrapper<WordStudy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .eq("is_mastered", 0) // 0表示未掌握
                   .eq("difficulty", difficulty); // 按难度筛选
        queryWrapper.last("LIMIT " + count);
        List<WordStudy> wordStudies = wordStudyMapper.selectList(queryWrapper);
        return wordStudies.stream()
                .map(wordStudy -> BeanUtil.copyProperties(wordStudy, WordStudyDTO.class))
                .collect(Collectors.toList());
    }
}